//
//  UserDateEditSeriesViewController.m
//  Molian
//
//  Created by 胡津 on 15/4/9.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "UserDateEditSeriesViewController.h"

@interface UserDateEditSeriesViewController ()<UITableViewDelegate,UITableViewDataSource>
//视图列表



@end

@implementation UserDateEditSeriesViewController
{
    NSInteger index;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);

    
   self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight)style:UITableViewStylePlain];
    self.tableView.backgroundColor = RGBA(244, 244, 244, 1);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollsToTop = YES;
    self.tableView.separatorColor = RGBA(225, 225, 225, 1);//分隔条颜色
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);//偏移量
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 51, 0);//滚动条偏移量
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    self.tableView.tableFooterView = [[UIView alloc] init];//不显示空的cell
    [self.view addSubview:self.tableView];

    index = 100;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString *CellIdentifier = @"SettingsCell" ;
    //自定义cell类
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if  (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        
    }
    //防止产生重影,首先移除子视图
    NSArray *subviews = [[NSArray alloc]initWithArray:cell.contentView.subviews];
    for (UIView *subview in subviews) {
        [subview removeFromSuperview];
    }
    
    cell.tintColor = APPCOLOR(1);
    
    UILabel *cellTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, DeviceWidth-100, 50)];
    cellTitle.textColor = RGBA(51, 51, 51, 1);
    cellTitle.font = [UIFont fontWithName:FontName size:16];
    cellTitle.text = [self.titleArr objectAtIndex:indexPath.row];
    cellTitle.highlightedTextColor = [UIColor whiteColor];
    [cell.contentView addSubview:cellTitle];
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = APPCOLOR(0.7);//点击后背景色

    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];


    index = indexPath.row;
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    
    if (index != 100) {
        if ([self.Delegate respondsToSelector:@selector(UserSeriesString:)]) {
            [self.Delegate UserSeriesString:self.titleArr[index]];
            NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:200 inSection:0];
            [self.tableView selectRowAtIndexPath:selectedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];

    int i = 0;
    for (NSString * str in self.titleArr)
    {
        i ++;
        if (self.nowSeriesStr) {
            if ([str isEqualToString:self.nowSeriesStr]){
                index = i-1;
                NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:i-1 inSection:0];
                [self.tableView selectRowAtIndexPath:selectedIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
                break;//一定要有break，否则会出错的。
            }
        }
    }
    
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_titleArr count];
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    self.delegate = nil;
}
@end
